/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JSJSYS
 */
public class yhy {
     //驱动类，H2jar包中可以找到
     static final String DRIVER_CLASS = "org.h2.Driver";
    //数据库连接URL，当前连接的是E:/H2目录下的mydb数据库
    //static final String JDBC_URL = "jdbc:h2:E://test";
      static final String JDBC_URL = "jdbc:h2:tcp://localhost/~/test";
     // static final String JDBC_URL = "jdbc:h2:tcp://localhost/mem:mydb";
    //用户名
     static final String USER = "sa";
    //密码
     static final String PASSWORD = "sa";
     private static int num =1;
    private static int getId(){
        return num++  ;
    }
public static void main(String args[]) {

         try {
             // create server
             Class.forName(DRIVER_CLASS);
         } catch (ClassNotFoundException ex) {
             Logger.getLogger(yhy.class.getName()).log(Level.SEVERE, null, ex);
         }
        // 根据连接URL，用户名，密码获取数据库连接
        Connection conn = null;
         try {
             conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
         } catch (SQLException ex) {
             Logger.getLogger(yhy.class.getName()).log(Level.SEVERE, null, ex);
         }
        // 创建一个Statement 
        Statement stmt = null;
         try {
             stmt = conn.createStatement();
         } catch (SQLException ex) {
             Logger.getLogger(yhy.class.getName()).log(Level.SEVERE, null, ex);
         }
         try {
             //如果存在表就先删除
             stmt.execute("DROP TABLE IF EXISTS stu_info");
         } catch (SQLException ex) {
             Logger.getLogger(yhy.class.getName()).log(Level.SEVERE, null, ex);
         }
         try {
             //创建stu_info表
             stmt.execute("CREATE TABLE stu_info(id VARCHAR(36) PRIMARY KEY,name VARCHAR(100),sex VARCHAR(4),student_id VARCHAR(50))");
             stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','程凌霄','男','20151101066')");
             stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','陈秋云','女','20151101026')");
             stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','黍路','女','20151101056')");
             stmt.executeUpdate("INSERT INTO stu_info VALUES('" + getId() + "','田梦','女','20151101096')"); // initServer();
        p("++++++++++++++++++++++++++++++++++++++显示刚保存的四条数据：");
        show(stmt);
        p("");
        p(""); 
        p("");   
        //删除 陈秋云
        stmt.executeUpdate("DELETE FROM stu_info WHERE name='陈秋云'");
        p("++++++++++++++++++++++++++++++++++++++删除 陈秋云 后，显示结果：");
        show(stmt);
        
        p("");
        p("");
        
        //修改 黍路 === >黎路
        stmt.executeUpdate("UPDATE stu_info SET name='黎路' WHERE name='黍路'");
        p("=+++++++++++++++++++++++++++++++++修改 黍路 === >黎路,显示结果：");
       // show(stmt);//释放stmt
        //stmt.close();
        
        
        //关闭连接
      //  conn.close();
        System.out.println("------jdbc -end!!!------");
         } catch (SQLException ex) {
             Logger.getLogger(yhy.class.getName()).log(Level.SEVERE, null, ex);
         }
         
}
       
 static void p(String a){
        System.out.println(a);
    }

    private static void show(Statement stmt) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
         // ++++++++++++++++++++++++++++++++ormlite
       




